package com.atues.service.impl;

import com.atues.domain.ResponseResult;
import com.atues.domain.entity.SysUser;
import com.atues.domain.vo.UserInfoVo;
import com.atues.emnu.AppHttpCodeEnum;
import com.atues.exception.SystemException;
import com.atues.utils.BeanCopyUtils;
import com.atues.utils.OssUtil;
import com.atues.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atues.mapper.SysUserMapper;

import com.atues.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;


/**
 * 用户表(SysUser)表服务实现类
 *
 * @author Liqun
 * @since 2024-09-05 16:07:03
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public ResponseResult register(SysUser user) {
//对数据进行非空判断
        if(!StringUtils.hasText(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getPassword())){
            throw new SystemException(AppHttpCodeEnum.PASSWORD_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getEmail())){
            throw new SystemException(AppHttpCodeEnum.EMAIL_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_NOT_NULL);
        }
//对数据进行是否存在的判断
        if(userNameExist(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        }
        if(nickNameExist(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_EXIST);
        }
//...
//对密码进行加密
        String encodePassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodePassword);
//存入数据库
        save(user);
        return ResponseResult.okResult();

    }

    private boolean nickNameExist(String nickName) {
        return  count(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getNickName,nickName)) > 0 ? true:false;
    }

    private boolean userNameExist(String userName) {
        return  count(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUserName,userName)) > 0 ? true:false;
    }

    @Override
    public ResponseResult userInfo() {
        //获取当前用户id
        Long userId = SecurityUtils.getUserId();
//根据用户id查询用户信息
        SysUser user = getById(userId);
//封装成UserInfoVo
        UserInfoVo vo = BeanCopyUtils.copyBean(user,UserInfoVo.class);
        return ResponseResult.okResult(vo);
    }

    @Override
    public ResponseResult uploadAvatar(MultipartFile file) {
//        OssUtil.uploadObjectOSS(null,)

        return null;
    }

    @Override
    public ResponseResult updateUserInfo(SysUser user) {
        updateById(user);
        return ResponseResult.okResult();
    }


}

